﻿using System.Activities;
using TransactionServer.General_Classes;
using System.ComponentModel;
using Microsoft.VisualBasic.Activities;


namespace TransactionServer.CRM4Activities
{
    [Designer(typeof(TransactionServer.ActivityDesign.CRMActivities.CRMGetEntity))]
    public sealed class CRM4GetEntity : CodeActivity
    {
        // Define an activity input argument of type string
        public InArgument<Context> Context { get; set; }
        public InArgument<string> Connection { get; set; }
        public InArgument<string> Dictionary { get; set; }
        public InArgument<string> Field { get; set; }
        public InArgument<string> Value { get; set; }

        public OutArgument<string> ResultId { get; set; }

        public CRM4GetEntity()
        {
            VisualBasicValue<Context> vbArgument = new VisualBasicValue<Context>(Constants.CONTEXT_NAME);
            this.Context = InArgument<Context>.FromExpression(vbArgument);
        }

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            // Obtain the runtime value of the Text input argument
            Context con = context.GetValue(this.Context);
            string Connection = context.GetValue(this.Connection);
            string Dictionary = context.GetValue(this.Dictionary);
            string Field = context.GetValue(this.Field);
            string Value = context.GetValue(this.Value);

            CRM4Connector conn = (CRM4Connector)con.Connector[Connection];

            string id = conn.lookupEntityByField(Dictionary, Field, Value);

            context.SetValue(ResultId, id);
        }
    }
}
